class CompaniesController < ApplicationController
    
  def new
    @company = Company.new 
    render :layout => "frontends"
  end
  
  def show
    @cu = current_user
    @cid = @cu.company_pid
    @company = Company.find_by_id(@cid)
    render :layout => nil;
  end
  
  def update
    @comp = Company.find_by_id(params[:items].split(",")[0])
    @comp.name = params[:items].split(",")[1]
    @comp.address = params[:items].split(",")[2]
    @comp.account = params[:items].split(",")[3]
    if @comp.save
      render :text => "0"
    else
      render :text => "1"
    end
  end
  
  def create
    companyparams = Hash.new
    companyparams[:name] = params[:companyname]
    companyparams[:address] = params[:address]
    companyparams[:account] = params[:account]
    userparams = Hash.new
    userparams[:name] = params[:username]
    userparams[:email] = params[:email]
    userparams[:password] = params[:password]
    userparams[:password_confirmation] = params[:password_confirmation]
    #userparams[:rolename_id] = 4
    
    @company = Company.new(companyparams)
    if @company.save
      userparams[:company_pid]=@company.id
      @user = User.new(userparams)    
      if @user.save
        Role.create(:rolename_id => 1, :company_id => @company.id, :ProductBacklog => true, :UserStoryCreate => true, :UserStoryDelete => true, :UserStoryEdit => true, :UserStoryAccept => true, :UserStoryPoker => true, :UserStoryToSprintBacklog => true, :SprintBacklog => true, :SprintBacklogTaskTake => true, :SprintBacklogTaskFinish => true)
        Role.create(:rolename_id => 2, :company_id => @company.id,  :ProductBacklog => true, :UserStoryCreate => true, :UserStoryDelete => true, :UserStoryEdit => true, :UserStoryAccept => true, :UserStoryPoker => true, :UserStoryToSprintBacklog => true, :SprintBacklog => true, :SprintBacklogTaskTake => true, :SprintBacklogTaskFinish => true)
        Role.create(:rolename_id => 3, :company_id => @company.id, :ProductBacklog => true, :UserStoryCreate => true, :UserStoryDelete => true, :UserStoryEdit => true, :UserStoryAccept => true, :UserStoryPoker => true, :UserStoryToSprintBacklog => true, :SprintBacklog => true, :SprintBacklogTaskTake => true, :SprintBacklogTaskFinish => true)
        Role.create(:rolename_id => 4, :company_id => @company.id,  :ProductBacklog => true, :UserStoryCreate => true, :UserStoryDelete => true, :UserStoryEdit => true, :UserStoryAccept => true, :UserStoryPoker => true, :UserStoryToSprintBacklog => true, :SprintBacklog => true, :SprintBacklogTaskTake => true, :SprintBacklogTaskFinish => true)
        Signup.new_company(@user).deliver
        redirect_to root_path , :notice => "Signed up!"
      else
        @company.delete
        render "new",:layout => "frontends"
      end
     else
      render "new",:layout => "frontends"
    end
  end
end
